/**
 * Copyright (c) 2009 Cycle Information Technology
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 
 * software and associated documentation files (the "Software"), to deal in the Software 
 * without restriction, including without limitation the rights to use, copy, modify, 
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to the following 
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies 
 * or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
package com.cycleit.cycleframework.model {
	import com.cycleit.cycleframework.controller.CycleController;
	import com.cycleit.cycleframework.entity.StateControlInfo;
	import com.cycleit.cycleframework.event.StateInfoChangeEvent;
	
	/**
	 * Modelo que contendrá toda la informacion necesaria para gestionar las vista de una 
	 * forma correcta.
	 */ 
	public class NavigationModel{
		
		//Instancia con el modelo
		private static var instance:NavigationModel = null;
		

		/**
		 * Crea y devuelve una instancia del NavigationModel 
		 */
		public static function getInstance():NavigationModel {
			if (instance == null){ 
				instance = new NavigationModel(new SingleClass());
			}
			return instance;
		}
		
		/**
		 * Constructor, inicializa las variables del modelo
		 *  
		 * @param v necesario para la correcta implementacion del Singleton
		 * 
		 */		
		public function NavigationModel(v:SingleClass) {
			_controller = new CycleController();
			navigationFlags = [];
			currentState = [];
			oldState = [];
		}
		
		
		/** controller */
		private var _controller:CycleController;
		
		public function get controller():CycleController {
			return _controller;
		}
		
		/** 
		 * Lista de flags que gestionan los cambios en la navegación. Cada componente navegable 
		 * añade su flag para su gestión interna 
		 * 
		 */
		public var navigationFlags:Array;
		/** navegación actual */
		public var currentState:Array;
		/** navegación anterior*/
		public var oldState:Array;
		
		/**
		 * Método utilizado por los NavigationView para saber
		 * su índice dentro del árbol de navegación de la aplicación
		 *   
		 * @return Number el índice que indica el nivel del árbol de navegación
		 * 
		 */		
		public function getNewIndex():Number{
			var aux:StateControlInfo = new StateControlInfo();
			navigationFlags[navigationFlags.length]=aux;
	
			aux.addEventListener(StateInfoChangeEvent.CHANGES_CHANGED, changesChangedHandler);
			return navigationFlags.length-1; 
		}
		
		private function changesChangedHandler(event:StateInfoChangeEvent):void {
			var flag:StateControlInfo = event.stateControlInfo;
			var index:Number = navigationFlags.indexOf(flag);
			if (flag.changes) {
				var nextIndex:uint = index+1;
				while(index<navigationFlags.length-1){
					navigationFlags[nextIndex].removeEventListener(StateInfoChangeEvent.CHANGES_CHANGED, changesChangedHandler);
					navigationFlags.splice(nextIndex,1);
				}
			}
		}
	}
}
class SingleClass{}